<!DOCTYPE html>
<html lang="en">
<head>
    <script src="https://cdn.staticfile.net/jquery/1.10.2/jquery.min.js">
    <script type="text/javascript" src="http://res.wx.qq.com/connect/zh_CN/htmledition/js/wxLogin.js"></script>
    <meta charset="UTF-8">
    <title>Title</title>
</head>
<body>
    <h1>微信扫码登录方式一</h1>
    <button onclick="getQrCode()" style="width: 100px;height: 50px;">获取二维码</button>
    <br>
    <img src="" id="qrCodeImgId"  style="width: 300px;height: 300px;display: none">
    <hr>
</body>
<script>
    //======================================微信扫码登录方式一=========================================================
    // 存储二维码标识,用于验证是否扫码成功
    var ticket;
    var t;
    // 获取登录二维码
    function getQrCode(){
        $.get('/getQrCode',function (data) {
            console.log("=============getQrCode=======================");
            console.log(data);
                ticket = data;
                // 二维码获取
                $('#qrCodeImgId').attr('src',"https://mp.weixin.qq.com/cgi-bin/showqrcode?ticket="+data);
                $('#qrCodeImgId').show();
                // 定时任务监听是否扫码成功
                t = window.setInterval(getOpenId,3000);

        });
    }

    // 扫码成功，获取用户openId=>为获取用户信息做准备
    function getOpenId() {
        $.get("wechatToken",{
            "ticket":ticket
        },function (data) {
            console.log("========getOpenId==========");
            console.log(data);
            if (data.openId) {
                window.clearInterval(t);

                alert("登录成功openId："+data.openId+"token:" + data.token);
                /**
                 * 1、第一次扫码登录进行账号绑定
                 * 2、以后根据openId获取用户信息
                 */
            }
        });
    }
</script>
</html>